<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +-----------------------------------------------------------------
// | 系统扩展库 (1.0)
// +-----------------------------------------------------------------
// | Copyright: (c) 2016 Genxin, Inc. All Rights Reserved.
// +-----------------------------------------------------------------
// | PHP version: 5.4
// +-----------------------------------------------------------------
// | Encoding: UTF-8
// +-----------------------------------------------------------------
// | Authors: Geng Xin <gengxin55200@126.com>
// +-----------------------------------------------------------------
//
// $Id: $

header("Content-Type:text/html; charset=UTF-8");

/**
 * 生成Excel文档
 *
 * @param $data
 * @param $title
 * @param string $filename
 */
function exportExcel($data, $title, $filename = '')
{
    import('Org.Util.PHPExcel');
    import('Org.Util.PHPExcel.Writer.Excel5');
    import('Org.Util.PHPExcel.IOFactory.php');

    $objPHPExcel = new \PHPExcel();
    $objProps = $objPHPExcel->getProperties();

    // 设置文件属性
    $objProps->setCreator('gengxin')
             ->setLastModifiedBy('gengxin')
             ->setTitle('Office 2007 XLSX Test Document')
             ->setSubject('Office 2007 XLSX Test Document')
             ->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')
             ->setKeywords('office 2007 openxml php')
             ->setCategory('Test result file');

    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);
    $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setName('Arial');
    $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);

    // 首个大写字母
    $showTitle = ord('A');
    for ($i = 0; $i < count($title); $i++) {
        // 从A1单元格开始
        $objPHPExcel->getActiveSheet(0)->setCellValue(chr($showTitle) . '1', $title[$i]);
        $showTitle++;
    }

    //遍历写入查询数据
    foreach ($data as $key => $val) {
        $showData = ord('A');
        foreach ($val as $k => $v) {
            $objPHPExcel->getActiveSheet(0)->setCellValue(chr($showData) . '' . ($key + 2), $v);
            $showData++;
        }
    }

    $filename = iconv('utf-8', 'gb2312', $filename);
    header('Content-Type: application/vnd.ms-excel;');
    header('Content-Disposition: attachment;filename=' . $filename . '.xls');
    header('Cache-Control: max-age=0');
    // 写入文件
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    // 下载文件
    $objWriter->save('php://output');
}

/**
 * 生成二维码
 *
 * @param $logo         logo图片
 * @param $QRImgName    二维码名称
 * @param $QRImgDir     二维码路径
 * @param $data         二维码内容
 * @param string $level 容错级别：L、M、Q、H
 * @param int $size     生成图片大小：1到10
 */
function generalQRCode($logo, $QRImgName, $QRImgDir, $data, $level = 'H', $size = 9)
{
    vendor("phpqrcode.phpqrcode");
    if (!file_exists($QRImgDir)) {
        mkdir($QRImgDir);
    }
    if (file_exists($logo)) {
        $QRBase = $QRImgDir . 'tmp.png';
        QRcode::png($data, $QRBase, $level, $size, 2);
        // 将创建出的png8二维码转为png24
        $QRBase = imagecreatefrompng($QRBase);
        $QR = imagecreatetruecolor($w = imagesx($QRBase), $h = imagesy($QRBase));
        imagecopy($QR, $QRBase, 0, 0, 0, 0, $w, $h);
        $logo = imagecreatefromstring(file_get_contents($logo));

        imagealphablending($logo, false);
        imagesavealpha($logo, true);

        imagealphablending($QR, false);
        imagesavealpha($QR, true);

        $QR_width       = imagesx($QR);
        $QR_height      = imagesy($QR);
        $logo_width     = imagesx($logo);
        $logo_height    = imagesy($logo);
        $logo_qr_width  = $QR_width / 4;
        $scale          = $logo_width / $logo_qr_width;
        $logo_qr_height = $logo_height / $scale;
        $from_width     = ($QR_width - $logo_qr_width) / 2;
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
        imagepng($QR, $QRImgDir . $QRImgName);
        unlink($QRImgDir . 'tmp.png');
    } else {
        $QRBase = $QRImgDir . $QRImgName;
        QRcode::png($data, $QRBase, $level, $size, 2);
        // 将创建出的png8二维码转为png24
        $QRBase = imagecreatefrompng($QRBase);
    }
}

/**
 * 微信分享
 */
function weixinShare()
{
    import('Org.Util.Jssdk');

    $jssdk = new \Jssdk(C('WECHAR.APP_KEY'), C('WECHAR.APP_SECRET'));
    $signPackage = $jssdk->GetSignPackage();

    return $signPackage;
}

/**
 * 微信支付记录
 *
 * @param $file
 * @param $word
 */
function wx_log_result($file, $word)
{
    $fp = fopen($file,"a");
    flock($fp, LOCK_EX) ;
    fwrite($fp,"执行日期：".strftime("%Y-%m-%d-%H：%M：%S",time())."\n".$word."\n\n");
    flock($fp, LOCK_UN);
    fclose($fp);
}

/**
 * 创建目录
 *
 * @param $dir
 * @return bool
 */
function createFolders($dir)
{
    return is_dir($dir) or (createFolders(dirname($dir)) and mkdir($dir, 0777) and @touch($dir.'/index.html'));
}

/**
 * 生成订单id
 *
 * @return string
 */
function generalOrderId()
{
    list($usec, $sec) = explode(' ', microtime());
    $num =  ((float) $usec  + (float) $sec ) * 10000;
    $num = strval($num);
    return 'DDZ' . $num;
}

/**
 * 生成会员码
 *
 * @param $member_id
 * @return mixed
 */
function generalMemberCode($member_id)
{
    $code = 'HYM'. $member_id.'-'.time() ;
    return $code;
}

/**
 * 生成比赛码
 *
 * @param $match_id
 * @return mixed
 */
function generalMatchCode($match_id)
{
    $code = 'BSM'. $match_id.'-'.time() ;

    return $code;
}